
# =============================================================
# File: App_E_asset.R
# Purpose: Produces results from Appendix Section E: Main Results with Fixed Assets Control
# Paper: Foreignness as an Asset: European Carbon Regulation and the Relocation Threat among Multinational Firms
# Author: Patrick Bayer, patrick.bayer@strath.ac.uk
# Date: 5 October 2022
#
# Data: ./data.csv
#
# Technical disclaimer:
# All analyses in R version 4.1.2 (2021-11-01)
# RStudio 2022.07.1 Build 554 ("Spotted Wakerobin" Release (7872775e, 2022-07-22) for Windows)
# Windows 10 Enterprise, 64-bit
# 12th Gen Intel(R) Core(TM) i7-1270P 2.20 GHz with 32GB RAM
# =============================================================

library(lmtest)
library(sandwich)
library(MASS)
library(MatchIt)
library(cem)
library(cobalt)
library(cowplot)
library(ggpubr)
library(marginaleffects)
library(AER)
library(tidyverse)


# Load data
df <- read_csv(file = "./data.csv")

# Trim sample down to true within firm sample
# (1) Limit sample to European MNCs: no allocation data for domestically-owned operations for non-EU MNCs
# (2) Exclude MNCs with only foreign-owned operations as there is no within MNC variation

df$sample <- ifelse(df$mnc.eu==1 & df$foreign.share<1,1,0)
df <- df[df$sample==1,]
df$foreign <- as.factor(df$foreign) # set variable as categorical

# =============================================================
# APPENDIX E:  Main results with fixed assets control
# =============================================================

# Construct plant-level fixed assets measure
df$plant.share.emit <- ifelse(df$firm.emit0>0, df$emit0/df$firm.emit0, NA)
summary(df$plant.share.emit)

df$fa.plant <- df$fixedassets*df$plant.share.emit
df$log.fa.plant <- ifelse(df$fa.plant>0, log(df$fa.plant), NA)

# Correlation between fixed assets and firm-level emissions
cor.test(df$fixedassets, df$firm.emit0)
cor.test(df$fa.plant, df$emit0)

# =============================================================
# Model (1)
# =============================================================

m1 <- lm(logDV~foreign+logAlloc0+logEmit0+log.fa.plant+as.factor(iso2)+as.factor(BVD), data=df)
summary(m1)

# Observations
nobs(m1)
length(m1$xlevels$`as.factor(iso2)`)
length(m1$xlevels$`as.factor(BVD)`)

# Marginal effects
(exp(summary(m1)$coef[["foreign1","Estimate"]])-1)*100

# Standard errors
(exp(coefci(m1, vcov. = vcovHC, type="HC0")["foreign1",])-1)*100 # robust SEs


# =============================================================
# Model (2)
# =============================================================

m2 <- lm(logDV~foreign+logAlloc0+logEmit0+log.fa.plant+as.factor(iso2)+as.factor(ETSsector)+as.factor(BVD), data=df)
summary(m2)

# Observations
nobs(m2)
length(m2$xlevels$`as.factor(ETSsector)`)
length(m2$xlevels$`as.factor(iso2)`)
length(m2$xlevels$`as.factor(BVD)`)

# Marginal effects
(exp(summary(m2)$coef[["foreign1","Estimate"]])-1)*100

# Standard errors
(exp(coefci(m2, vcov. = vcovHC, type="HC0")["foreign1",])-1)*100 # robust SEs


# =============================================================
# Model (3)
# =============================================================

# Exact matching on 4-digit NACE code and firm
df$NACE <- ifelse(df$NACE=="",NA,df$NACE) # Recode empty strings as NA 

m <- matchit(foreign ~ NACE + BVD, data=df[is.na(df$NACE)==FALSE,], method="exact")
df.match <- match.data(m)

# Re-run main model on matched sample
m3 <- lm(logDV~foreign+logAlloc0+logEmit0+log.fa.plant+as.factor(iso2)+as.factor(ETSsector)+as.factor(BVD), data=df.match, weights=weights)
summary(m3)

# Observations
nobs(m3)
length(m3$xlevels$`as.factor(ETSsector)`)
length(m3$xlevels$`as.factor(iso2)`)
length(m3$xlevels$`as.factor(BVD)`)

# Marginal effects
(exp(summary(m3)$coef[["foreign1","Estimate"]])-1)*100

# Standard errors
(exp(coefci(m3, vcov. = vcovHC, type="HC0")["foreign1",])-1)*100 # robust SEs




# =============================================================
#                       END OF CODE
# =============================================================